<?php

namespace addons\shopro\model;

use think\Model;

/**
 * 认领
 */
class UserClaim extends Model
{

    // 表名,不含前缀
    protected $name = 'shopro_user_claim';
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';
    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    // 追加属性
    protected $append = [
        'is_water_text',
        'is_feeding_text',
        'is_sweep_text',
        'createtime_text'
    ];

    public function user()
    {
        return $this->belongsTo(User::class, 'user_id', 'id');
    }

    public function goods()
    {
        return $this->belongsTo(\app\admin\model\nc\Goods::class, 'nc_goods_id', 'id');
    }

    public function getIsWaterTextAttr($value,$data){
        if(isset($data['id'])){
            $model = new UserWalletLog();
            $value = $model->where(['item_id'=>$data['id'],'wallet_type'=>'score','type'=>'nc_water'])->whereTime('createtime','d')->count();
        }
        return $value;
    }

    public function getIsFeedingTextAttr($value,$data){
        if(isset($data['id'])){
            $model = new UserWalletLog();
            $value = $model->where(['item_id'=>$data['id'],'wallet_type'=>'score','type'=>'nc_feeding'])->whereTime('createtime','d')->count();
        }
        return $value;
    }

    public function getIsSweepTextAttr($value,$data){
        if(isset($data['id'])){
            $model = new UserWalletLog();
            $value = $model->where(['item_id'=>$data['id'],'wallet_type'=>'score','type'=>'nc_sweep'])->whereTime('createtime','d')->count();
        }
        return $value;
    }

    public function getCreatetimeTextAttr($value,$data){
        if(isset($data['createtime'])){
            $value = date("Y-m-d",$data['createtime']);
        }
        return $value;
    }

}
